﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToRTR.YAML.CApExportToRTRYamlMgr
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToRTR.YAML;

public class CApExportToRTRYamlMgr
{
  internal Dictionary<ITxObject, IObj> m_Tx2YamlObject = new Dictionary<ITxObject, IObj>();
  private Dictionary<string, IObj> m_Tx2YamlRobotObject = new Dictionary<string, IObj>();
  private static CApExportToRTRYamlMgr m_instance;
  private Dictionary<Type, Action<IObj>> m_typeDic;

  public Dictionary<string, TxTransformation> JointsOffset { get; } = new Dictionary<string, TxTransformation>();

  public Dictionary<TxRobot, TxJoint[]> ExternalJoints { get; } = new Dictionary<TxRobot, TxJoint[]>();

  public Dictionary<TxRobot, Tuple<List<ITxObject>, List<ITxObject>>> IgnoredObjects { get; } = new Dictionary<TxRobot, Tuple<List<ITxObject>, List<ITxObject>>>();

  public Workcell Workcell { get; set; } = new Workcell();

  public static CApExportToRTRYamlMgr Instance
  {
    get
    {
      if (CApExportToRTRYamlMgr.m_instance == null)
        CApExportToRTRYamlMgr.m_instance = new CApExportToRTRYamlMgr();
      return CApExportToRTRYamlMgr.m_instance;
    }
  }

  private CApExportToRTRYamlMgr()
  {
    this.m_typeDic = new Dictionary<Type, Action<IObj>>();
    this.m_typeDic.Add(typeof (Frame), (Action<IObj>) (yamlObj => this.Workcell.Frames.Add(yamlObj as Frame)));
    this.m_typeDic.Add(typeof (RobotInstance), (Action<IObj>) (yamlObj => this.Workcell.Robots.Add(yamlObj as RobotInstance)));
    this.m_typeDic.Add(typeof (Mate), (Action<IObj>) (yamlObj => this.Workcell.Mates.Add(yamlObj as Mate)));
    this.m_typeDic.Add(typeof (ObjectInstance), (Action<IObj>) (yamlObj => this.Workcell.Objects.Add(yamlObj as ObjectInstance)));
    this.m_typeDic.Add(typeof (Joint), (Action<IObj>) (yamlObj => this.Workcell.Joints.Add(yamlObj as Joint)));
  }

  public void Clear()
  {
    this.Workcell = new Workcell();
    this.JointsOffset.Clear();
    this.ExternalJoints.Clear();
    this.IgnoredObjects.Clear();
    this.m_Tx2YamlObject.Clear();
    this.m_Tx2YamlRobotObject.Clear();
  }

  public void AddObj<T>(T yamlObj, ITxObject txObj = null) where T : IObj
  {
    if (txObj != null)
      this.m_Tx2YamlObject[txObj] = (IObj) yamlObj;
    this.m_typeDic[yamlObj.GetType()]((IObj) yamlObj);
  }

  public void AddRobotPrototype(RobotPrototype yamlObj) => this.Workcell.RobotModels.Add(yamlObj);

  public void AddRobotKinematicChild<T>(RobotPrototype yamlRobot, T yamlObj, ITxObject txObj) where T : IObj
  {
    this.m_Tx2YamlObject[txObj] = (IObj) yamlObj;
    this.m_Tx2YamlRobotObject[CApExportToRTRYamlUtils.GetRobotKinematicChildKey(yamlRobot.Name, txObj)] = (IObj) yamlObj;
    if (yamlObj.GetType() == typeof (Link))
    {
      yamlRobot.Links.Add((object) yamlObj as Link);
    }
    else
    {
      if (!(yamlObj.GetType() == typeof (Joint)))
        return;
      yamlRobot.Joints.Add((object) yamlObj as Joint);
    }
  }

  public IObj GetObj(ITxObject txObj)
  {
    IObj obj = (IObj) null;
    if (this.m_Tx2YamlObject.ContainsKey(txObj))
      obj = this.m_Tx2YamlObject[txObj];
    return obj;
  }

  public IObj GetRobotKinematicChild(RobotPrototype yamlRobot, ITxObject txObj)
  {
    IObj robotKinematicChild = this.GetObj(txObj);
    if (robotKinematicChild == null)
    {
      string kinematicChildKey = CApExportToRTRYamlUtils.GetRobotKinematicChildKey(yamlRobot.Name, txObj);
      if (this.m_Tx2YamlRobotObject.ContainsKey(kinematicChildKey))
        robotKinematicChild = this.m_Tx2YamlRobotObject[kinematicChildKey];
    }
    return robotKinematicChild;
  }

  public Mate CreateMateToReferenceFrame(ITxLocatableObject obj, IObjectInstance yamlObj)
  {
    TxTransformation transformation = CApExportToRTRExportManager.UseWorkingFrame && !TxApplication.ActiveDocument.WorkingFrame.Equals((object) new TxTransformation()) ? obj.AbsoluteLocation.LocationRelativeToWorkingFrame : obj.AbsoluteLocation;
    Mate yamlObj1 = new Mate()
    {
      Name = "Origin_" + TxObjectEx.GetUniqueNameForRTR((ITxObject) obj, false),
      Parent = new ObjectRef() { Origin = "~" },
      Child = yamlObj.CreateObjectRef(),
      OffsetXyz = new Vec3D(transformation.Translation),
      OffsetRpy = new Vec3D(transformation.GetRotation())
    };
    this.AddObj<Mate>(yamlObj1);
    return yamlObj1;
  }
}
